package com.oromwin.repos.common;

import java.io.IOException;
import java.util.Properties;

import java.io.FileInputStream;
import java.io.InputStream;

public class ReposClientConfig {
    private static ReposClientConfig INSTANCE;
    static {
        INSTANCE = new ReposClientConfig();
    }
    private static int DEFAULT_BUFFER_SIZE = 4096;
    private static int DEFAULT_TIMEOUT = 60000;

    private String reposSocketServerIP = null;
    private int reposSocketServerPort = 0;
    private int reposSocketIOBuffSize = DEFAULT_BUFFER_SIZE;
    private int reposSocketClientTimeout = DEFAULT_TIMEOUT;
    private boolean DEBUG = false;

    private ReposClientConfig() {
        if (DEBUG) { // XXX: 공유모듈로 사용될 예정이므로, 상위 클래스에서 상속 받아 사용하므로, 로컬설정은 필요없음
            loadProperties(Common.DEFAULT_PATH);
        }
    }

    public static ReposClientConfig getInstance() {
        return INSTANCE;
    }

    public static int getDefaultTimeout() {
        return DEFAULT_TIMEOUT;
    }

    public static int getDefaultBufferSize() {
        return DEFAULT_BUFFER_SIZE;
    }

    public String getReposSocketServerIP() {
        return reposSocketServerIP;
    }
    public int getReposSocketPort() {
        return reposSocketServerPort;
    }
    public int getReposSocketIOBuffSize() {
        return reposSocketIOBuffSize;
    }
    public int getReposSocketClientTimeout() {
        return reposSocketClientTimeout;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("-------------------- Repository Config --------------------\n");
        sb.append("repos.socket.server.ip=" + reposSocketServerIP + "\n");
        sb.append("repos.socket.server.port=" + reposSocketServerPort + "\n");
        sb.append("repos.socket.client.timeout=" + reposSocketClientTimeout + "\n");
        sb.append("repos.socket.io.buff.size=" + reposSocketIOBuffSize + "\n");
        sb.append("-------------------- Repository Config --------------------\n");
        return sb.toString();
    }

    private void loadConfig(InputStream is) throws IOException, NumberFormatException {
        Properties p = new Properties();
        p.load(is);
        reposSocketServerIP = p.getProperty("repos.socket.server.ip");
        reposSocketServerPort = Integer.parseInt(p.getProperty("repos.socket.server.port"));
        reposSocketClientTimeout = Integer.parseInt(p.getProperty("repos.socket.client.timeout"));
        reposSocketIOBuffSize = Integer.parseInt(p.getProperty("repos.socket.io.buff.size"));
    }

    public void loadProperties(String filepath) {
        InputStream is = null;
        try {
            is = new FileInputStream(filepath);
            loadConfig(is);
            is.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (NumberFormatException nfe) {
            nfe.printStackTrace();
        } finally {
            try {
                if (is != null) is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
